<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class PropertiesModelProperties extends JModel
{
	var $_data;
	var $TableName = null;
	var $id_provincia = null;
	var $id_ciudad = null;
	var $CatID = null;
	var $sqlProvincia = null;
	var $Buscar = null;
var $_total = null;
var $_pagination = null;

function __construct()
  {
 	parent::__construct();
	global $mainframe, $option;

$component = JComponentHelper::getComponent( 'com_properties' );
$params = new JParameter( $component->params );
$this->Mostrar = $params->get( 'cantidad_productos' ) ;
$this->ProvinciaDefecto = $params->get( 'ProvinciaDefecto' ) ;
	
if(!JRequest::getVar('limitstart')){
	$this->setState('limit', $this->Mostrar);
	$this->setState('limitstart', 0);
}else{
	$limit = $this->Mostrar;
	$this->setState('limit', $this->Mostrar);
$limitstart = JRequest::getVar('limitstart');
$this->setState('limitstart', $limitstart);	
$start = JRequest::getVar('start');
$this->setState('start', $start);	
}



	$estaProvincia = JRequest::getVar('provid', 0, '', 'int');
	$estaCiudad =JRequest::getVar('ciuid', 0, '', 'int');
	$esteTipo =JRequest::getVar('tid', 0, '', 'int');
	$estaCategoria =JRequest::getVar('cid', 0, '', 'int');
	$esteProducto = JRequest::getVar('id', 0, '', 'int');

	$this->setId($estaProvincia,$estaCiudad,$esteTipo,$estaCategoria,$esteProducto);
///tomar parametros
$this->pathway();
  }
  
  function setId($estaProvincia,$estaCiudad,$esteTipo,$estaCategoria,$esteProducto)
	{
		// Set id and wipe data
		$this->_id_estaProvincia	= $estaProvincia;
		$this->_id_estaCiudad		= $estaCiudad;
		$this->_id_esteTipo			= $esteTipo;
		$this->_id_estaCategoria	= $estaCategoria;
		$this->_id_esteProducto		= $esteProducto;
		$this->_data_nombres		= null;		
	}

function pathway()
	{
	global $mainframe;	
	$breadcrumbs = & $mainframe->getPathWay();	

	if($this->_id_estaProvincia){
$query = ' SELECT pr.*, '
.' CASE WHEN CHAR_LENGTH(pr.alias) THEN CONCAT_WS(":", pr.id_provincia, pr.alias) ELSE pr.id_provincia END as provslug'
.' FROM #__propiedades_provincias as pr '
.' WHERE id_provincia = '.$this->_id_estaProvincia;
$this->_db->setQuery( $query );
$this->_dataProv = $this->_db->loadObject($query);
$breadcrumbs->addItem( $this->_dataProv->nombre_provincia, JRoute::_( 'index.php?option=com_propiedades&view=propiedades&provid='.$this->_dataProv->provslug) );
	}	
	if($this->_id_estaCiudad){
$query = ' SELECT c.*, '
.' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id_ciudad, c.alias) ELSE c.id_ciudad END as ciuslug'
.' FROM #__propiedades_ciudades as c '
.' WHERE id_ciudad = '.$this->_id_estaCiudad;
$this->_db->setQuery( $query );
$this->_dataCiu = $this->_db->loadObject($query);
$breadcrumbs->addItem( $this->_dataCiu->nombre_ciudad, JRoute::_( 'index.php?option=com_propiedades&view=propiedades&provid='.$this->_dataProv->provslug.'&ciuid='.$this->_dataCiu->ciuslug) );
	}

	if($this->_id_estaCategoria){	
$query = ' SELECT t.*, '
.' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as catslug'
.' FROM #__properties_category as t '
.' WHERE t.id = '.$this->_id_estaCategoria;
$this->_db->setQuery( $query );
$this->_dataCat = $this->_db->loadObject($query);
	
	$breadcrumbs->addItem( $this->_dataCat->name, JRoute::_( 'index.php?option=com_properties&view=properties&cid='.$this->_dataCat->catslug) );
	}
	
	if($this->_id_esteTipo){
$query = ' SELECT t.*, '
.' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug'
.' FROM #__properties_type as t '
.' WHERE t.id = '.$this->_id_esteTipo;
$this->_db->setQuery( $query );
$this->_dataTipo = $this->_db->loadObject($query);
$breadcrumbs->addItem( $this->_dataTipo->name, JRoute::_( 'index.php?option=com_proerties&view=properties&cid='.$this->_dataCat->catslug.'&tid='.$this->_dataTipo->Tslug) );
	}


	}

function _buildQuery()
	{	
	if(JRequest::getVar('cid')){
		$this->sqlCategoryLeft = ' LEFT JOIN #__properties_product_category AS pc ON p.id = pc.productid LEFT JOIN #__properties_category AS c ON c.id = pc.categoryid';
		$this->sqlCategory = ' AND pc.categoryid = '.JRequest::getVar('cid', 0, '', 'int');
}else{
		$this->sqlCategoryLeft = ' LEFT JOIN #__properties_category AS c ON c.id = p.parent';
}
	if(JRequest::getVar('tid')){
		$this->sqlType = ' AND p.type = '.JRequest::getVar('tid', 0, '', 'int');
}
			$this->_query = ' SELECT p.*,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,pf.name as name_profile,pf.logo_image as logo_image_profile, '//pc.productid as idpc, '
		. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
		. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
		. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
		. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sdslug,'		
		. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, '	
		. ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Typeslug '			
				. ' FROM #__properties_products AS p '				
				. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '				
				. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
				. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
				. ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '
				. $this->sqlCategoryLeft
				//. ' LEFT JOIN #__properties_product_category AS pc ON p.id = pc.productid '
				//. ' LEFT JOIN #__properties_category AS c ON c.id = pc.categoryid '
				. ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
				. ' WHERE p.published = 1 '	
			//	. $this->sqlProvincia.' '
			//	. $this->sqlProvinciaDefecto.' '
			//	. $this->sqlCiudad.' '
				. $this->sqlType.' '
				. $this->sqlCategory				
				.' ORDER BY p.id DESC'			
				;
//echo str_replace('#_','jos',$this->_query);
        return $this->_query;		
}

function _buildQueryAgentListing()
	{	

			$this->_query = ' SELECT p.*,c.name as name_category,cy.name as name_country,s.name as name_state,l.name as name_locality,pf.name as name_profile,pf.logo_image as logo_image_profile, '
		. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
		. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
		. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
		. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sdslug,'		
		. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug '				
				. ' FROM #__properties_products AS p '				
				. ' LEFT JOIN #__properties_category AS c ON c.id = p.parent '
				. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '				
				. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
				. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
				. ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '
				. ' WHERE p.published = 1 '	
				. ' AND agent_id = '.JRequest::getVar('aid')
			//	. $this->sqlProvincia.' '
			//	. $this->sqlProvinciaDefecto.' '
			//	. $this->sqlCiudad.' '
			//	. $this->sqlSector.' '
			//	. $this->sqlCategoria.' '								
				.' ORDER BY p.id DESC'			
				;
//echo str_replace('#_','jos',$this->_query);
        return $this->_query;		
}

function _buildQueryShortList()
	{	
 	$user =& JFactory::getUser();	
			$this->_query = ' SELECT p.*,lb.propid,lb.uid,c.name as name_category,cy.name as name_country,s.name as name_state,l.name as name_locality,pf.name as name_profile,pf.logo_image as logo_image_profile, '
		. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
		. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
		. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
		. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sdslug,'		
		. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug '				
				. ' FROM #__properties_products AS p '				
				. ' LEFT JOIN #__properties_category AS c ON c.id = p.parent '
				. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '				
				. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
				. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
				. ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '
				. ' LEFT JOIN  #__properties_lightbox AS lb ON p.id = lb.propid '
				. ' WHERE p.published = 1 '	
				. ' AND lb.propid = p.id '
				. ' AND lb.uid = '.$user->id
			//	. $this->sqlProvincia.' '
			//	. $this->sqlProvinciaDefecto.' '
			//	. $this->sqlCiudad.' '
			//	. $this->sqlSector.' '
			//	. $this->sqlCategoria.' '								
				.' ORDER BY p.id DESC'			
				;
				
				//	$querysl = ' SELECT p.*,lb.* FROM #__properties_products AS p LEFT JOIN  #__properties_lightbox AS lb ON p.agent_id = lb.uid WHERE lb.propid = p.id AND lb.uid = '.$user->id;
					
//echo str_replace('#_','jos',$this->_query);
        return $this->_query;		
}




function _buildQuerySearch()
	{	
if(JRequest::getVar('cyid')){
$this->sqlCountry = ' AND p.cyid = '.JRequest::getVar('cyid', 0, '', 'int');
//echo 'cyid: '.JRequest::getVar('cyid', 0, '', 'int');
}

if(JRequest::getVar('sid')){
$this->sqlProvincia = ' AND p.sid = '.JRequest::getVar('sid', 0, '', 'int');
}

if(JRequest::getVar('lid')){
$this->sqlCiudad = ' AND p.lid = '.JRequest::getVar('lid', 0, '', 'int');
}

if(JRequest::getVar('cid')){
$this->sqlCategoria = ' AND p.parent = '.JRequest::getVar('cid', 0, '', 'int');
}

if(JRequest::getVar('tid')){
$this->sqltype = ' AND p.type = '.JRequest::getVar('tid', 0, '', 'int');
}

if(JRequest::getVar('id_minprice')){
//echo 'minprice : '.JRequest::getVar('id_minprice');
$this->sqlprecio_producto = ' AND p.price BETWEEN '.JRequest::getVar('id_minprice').
' AND '.JRequest::getVar('id_maxprice');
}

if(JRequest::getVar('id_maxprice')){
}

if(JRequest::getVar('id_bedrooms')){
$this->sqldormitorios = ' AND p.bedrooms >= '.JRequest::getVar('id_bedrooms');
}

if(JRequest::getVar('id_bathrooms')){
$this->sqlbathrooms = ' AND p.bathrooms >= '.JRequest::getVar('id_bathrooms');
}

if(JRequest::getVar('id_garage')){
$this->sqlparking = ' AND p.garage >= '.JRequest::getVar('id_garage');
}


if($this->ProvinciaDefecto != 0){$this->sqlProvinciaDefecto = ' AND p.provincia_producto = '.$this->ProvinciaDefecto;}

if(!JRequest::getVar('textoBuscar') || JRequest::getVar('textoBuscar')==JText::sprintf('TODAS LAS PROPIEDADES')){
$this->sqltextoBuscar = '';
}else{
$this->sqltextoBuscar = ' AND ( p.titulo_producto LIKE \'%'.JRequest::getVar('textoBuscar').'%\' )';}
//echo '$textoBuscar: '.$textoBuscar.'<br>';

//thuan add search
if(JRequest::getVar('address')){
$address = " AND p.adress like '%".JRequest::getVar('address')."%'";
}
//end

$this->_queryS = ' SELECT p.*,c.name as name_category,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,pf.name as name_profile,pf.logo_image as logo_image_profile, '
				//. ' pc.productid as idpc, '
				. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
	        	. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
				. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, t.alias) ELSE s.id END as Stateslug,'
				. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Locslug,'
				. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Catslug,'		
				. ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Typeslug '				
				. ' FROM #__properties_products AS p '				
				. ' LEFT JOIN #__properties_category AS c ON c.id = p.parent '
				//. ' LEFT JOIN #__properties_product_category AS pc ON p.id = pc.productid '
				. ' LEFT JOIN #__properties_type AS t ON t.id = p.type '				
				. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
				. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
				. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '
				. ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '	
				. ' WHERE p.published = 1 '
				. $this->sqlCountry.' '
				. $this->sqlProvincia.' '
				//. $this->sqlProvinciaDefecto.' '
				. $this->sqlCiudad.' '
				. $this->sqlSector.' '
				. $this->sqlCategoria.' '
				. $this->sqltype.' '
				. $this->sqlprecio_producto.' '
				. $this->sqldormitorios.' '
				. $this->sqlbathrooms.' '
				. $this->sqlparking.' '
				. $address.' '					//thuan
				.' ORDER BY p.id DESC'			
				;
//echo str_replace('#_','jos',$this->_queryS);
//print_r($this->_query);
return $this->_queryS;

}
function getItemid()
	{
	$query = 'SELECT id FROM #__menu' .
				' WHERE LOWER( link ) = "index.php?option=com_properties&view=properties"';
				
		$this->_db->setQuery( $query );
		$this->row = $this->_db->loadObject();
		
		return $this->row;
	}	
		
function getLast10Products()
	{	
	 $Prodquery = ' SELECT p.*,c.name as name_category,cy.name as name_country,s.name as name_state,l.name as name_locality, '
		. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
		. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
		. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
		. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sdslug,'		
		. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug '				
				. ' FROM #__properties_products AS p '				
				. ' LEFT JOIN #__properties_category AS c ON c.id = p.parent '
				. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '				
				. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
				. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
				. ' WHERE p.published = 1 '	
				. ' LIMIT 10';
	
	 $this->_db->setQuery( $Prodquery );
	 $this->_Proddata = $this->_getList($Prodquery);
	 return $this->_Proddata;
	
	
	}
	
	
	
	
	
function getData() 
  {  
 	// if data hasn't already been obtained, load it
 	if (empty($this->_data)) {	
 	    $query = $this->_buildQuery();
 	    $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));	 	
	}
//	print_r($this->_data);
 	return $this->_data;
  }
  
function getDataBuscar() 
  {  
 	// if data hasn't already been obtained, load it
 	if (empty($this->_data)) {	
 	    $query = $this->_buildQuery();
 	    $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit')); 	
	}
 	return $this->_data;
  }

function getDataAgent() 
  {  
  $Agent=JRequest::getVar('aid');
 	// if data hasn't already been obtained, load it
 	$queryA = 'SELECT * FROM #__properties_profiles ' .
			' WHERE mid = '.$Agent;
				
		$this->_db->setQuery( $queryA );
		$this->row = $this->_db->loadObject();
		
		return $this->row;
  }



function getDataAgentListing() 
  {  
 	// if data hasn't already been obtained, load it
 	if (empty($this->_data)) {	
 	    $query = $this->_buildQueryAgentListing();
 	    $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit')); 	
	}
 	return $this->_data;
  }
  
  
function getTotalAgentListing()
  {
 	// Load the content if it doesn't already exist
 	if (empty($this->_total)) {
 	    $queryS = $this->_buildQueryAgentListing();
 	    $this->_total = $this->_getListCount($queryS);	
 	}
 	return $this->_total;
  }

function getPaginationAgentListing()
  {
 	// Load the content if it doesn't already exist
 	if (empty($this->_pagination)) {
 	    jimport('joomla.html.pagination');
 	    $this->_pagination = new JPagination($this->getTotalAgentListing(), $this->getState('limitstart'), $this->getState('limit') );		
		
 	}
 	return $this->_pagination;
  }  
  
function getTotal()
  {
 	// Load the content if it doesn't already exist
 	if (empty($this->_total)) {
 	    $query = $this->_buildQuery();
 	    $this->_total = $this->_getListCount($query);	
 	}
 	return $this->_total;
  }


function getPagination()
  {
 	// Load the content if it doesn't already exist
 	if (empty($this->_pagination)) {
 	    require_once( JPATH_COMPONENT.DS.'helpers'.DS.'pagination.php' );
 	    $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );		
		
 	}
 	return $this->_pagination;
  }


function getDataSearch() 
  {  
 	// if data hasn't already been obtained, load it
 	if (empty($this->_data)) {	
 	    $query = $this->_buildQuerySearch();
 	    $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));	 	
	}
 	return $this->_data;
  }
  
function getTotalSearch()
  {
 	// Load the content if it doesn't already exist
 	if (empty($this->_total)) {
 	    $queryS = $this->_buildQuerySearch();
 	    $this->_total = $this->_getListCount($queryS);	
 	}
 	return $this->_total;
  }

  
 function getPaginationSearch()
  {
 	// Load the content if it doesn't already exist  echo CatTreeHelper::MultiParent( $this->datos,'category' ); 
 	if (empty($this->_pagination)) {
 	    
		require_once( JPATH_COMPONENT.DS.'helpers'.DS.'pagination.php' );
 	    $this->_pagination = new JPagination($this->getTotalSearch(), $this->getState('limitstart'), $this->getState('limit') );		
		
 	}
 	return $this->_pagination;
  } 
  
  
  

   
function getDataShortList() 
  {    
  $querysl = $this->_buildQueryShortList();				
	$this->_db->setQuery( $querysl );
	$this->_data = $this->_getList($querysl, $this->getState('limitstart'), $this->getState('limit'));		
	return $this->_data;
  }
  
 function getTotalShortList()
  {
 	if (empty($this->_total)) {
	$querysl = $this->_buildQueryShortList();
// 	    $querysl = ' SELECT p.*,lb.* FROM #__properties_products AS p LEFT JOIN  #__properties_lightbox AS lb ON p.agent_id = lb.uid WHERE lb.propid = p.id AND lb.uid = '.$user->id;	
			
 	    $this->_total = $this->_getListCount($querysl);	
 	}
 	return $this->_total;
  }  
  
 function getPaginationShortList()
  {
 	// Load the content if it doesn't already exist
 	if (empty($this->_pagination)) {
 	    require_once( JPATH_COMPONENT.DS.'helpers'.DS.'pagination.php' );
 	    $this->_pagination = new JPagination($this->getTotalShortList(), $this->getState('limitstart'), $this->getState('limit') );		
		
 	}
 	return $this->_pagination;
  } 










}//fin clase